package com.rosiminc.pm.model;

import java.util.Observable;

import com.rosiminc.pm.game.*;
import com.rosiminc.pm.game.tiles.*;

/**
 * This model encapsulates a PipesMania game board tile.
 * 
 * @author Shymon
 * 
 */
public class PipesManiaTileModel extends Observable {

	private GameBoardTile tile;
	private PipesManiaGameModel game;

	/**
	 * This constructor creates a new tile using a tile and the game model.
	 * 
	 * @param tile
	 *            the tile
	 * @param game
	 *            the game model
	 */
	public PipesManiaTileModel(GameBoardTile tile, PipesManiaGameModel game) {
		if (tile == null)
			throw new IllegalArgumentException("Null tile sent");
		if (game == null)
			throw new IllegalArgumentException("Null game sent");
		this.game = game;
		this.tile = tile;
	}

	/**
	 * This method returns the tile.
	 * 
	 * @return the tile
	 */
	public Tile getTile() {
		return tile.getTile();
	}

	/**
	 * This method returns the tile's type.
	 * 
	 * @return the tile's type
	 */
	public TileType getTileType() {
		return tile.getTileType();
	}

	/**
	 * This method returns whether the tile is solved.
	 * 
	 * @return if tile is solved
	 */
	public boolean isSolved() {
		return tile.getLastSolved();
	}

	/**
	 * This method performs the tile's primary action.
	 */
	public void actionA() {
		game.actionA(tile.getRow(), tile.getColumn());
		actionChecks();
	}

	/**
	 * This method performs the tile's secondary action.
	 */
	public void actionB() {
		game.actionB(tile.getRow(), tile.getColumn());
		actionChecks();
	}

	/**
	 * This method performs checks and alerts after an action is performed.
	 */
	private void actionChecks() {
		alertObservers(getTile());
		alertObservers(tile.getLastSolved());
		game.checkIfOver();
	}

	/**
	 * This method alerts the observers with a specified signal.
	 * 
	 * @param o
	 *            the signal
	 */
	private void alertObservers(Object o) {
		setChanged();
		notifyObservers(o);
	}
}
